package com.cash.channel.svc.logic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.repay.WdOrderInfoModel;
import com.cash.channel.api.model.enums.HomeLoanTypeEnum;
import com.cash.channel.svc.util.SortWdOrderDate;
import com.cash.user.service.UserService;
import com.geex.haneda.HanedaException;
import com.geex.haneda.model.api.ApiReqModel;
import com.geex.haneda.model.api.ApiResModel;
import com.geex.haneda.service.ApiService;
import com.google.common.collect.Maps;
import geex.core.service.LoanService;
import geex.core.service.model.emuns.IsOverdueEnum;
import geex.core.service.model.emuns.LoanStatusEnum;
import geex.core.service.model.loan.Loan;
import geex.core.service.model.loan.RepayPlan;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author
 */
@Slf4j
@Component
public class OrderLogic {
    @Autowired(required = false)
    ApiService hanedaApiService;



    public Map<String, Object> secUsersOrderInfoAPI(String uid) throws HanedaException {
        JSONObject object = new JSONObject();
        object.put("cUuid", uid);
        String param = object.toString();
        ApiReqModel apiReqModel = new ApiReqModel("secUsersOrderInfoAPI", param);
        ApiResModel apiResModel = hanedaApiService.api(apiReqModel);
        if (apiResModel != null && apiResModel.getResult() != null && apiResModel.getSuccess()) {
            Map<String, Object> resultMap = JSONObject.parseObject(apiResModel.getResult());
            log.info("根据uid查认证单 secUsersOrderInfoAPI:" + resultMap);
            return resultMap;
        } else {
            log.error("风控端：查询人的认证订单信息多表合并 提供订单号 当前步数:" + apiResModel.getErrMessage());
            throw new HanedaException(apiResModel.getErrMessage());
        }
    }

    /**
     * 查询订单授权信息 byappid
     *
     * @param appId
     * @return
     * @throws HanedaException
     */
    public Map<String, Object> secQCOrderBAAPI(String appId) throws HanedaException {
        try {
            log.info("风控端：查询订单授权信息:" + appId);
            JSONObject requestObj = new JSONObject();
            requestObj.put("cAppId", appId);
            ApiResModel apiResModel = hanedaApiService.api(new ApiReqModel("secQCOrderBAAPI", requestObj.toString()));
            if (!apiResModel.getSuccess()) {
                log.error("风控端：查询订单授权信息失败:" + appId + "\t" + apiResModel.getErrMessage());
                throw new HanedaException(apiResModel.getErrMessage());
            }
            Map<String, Object> resultMap = JSONObject.parseObject(apiResModel.getResult());
            log.info("风控端：查询订单授权信息:" + apiResModel.getResult());
            return resultMap;
        } catch (Exception ex) {
            log.error("风控端：查询订单授权信息失败:" + appId + "\t" + ex.getMessage(), ex);
            throw ex;
        }
    }

    public Map<String, Object> findWithdrawAppInfoByAppId(String wdId) throws CashChannelException {
        log.info("findWithdrawAppInfoByUid, wdId:" + wdId);
        JSONObject requestObj = new JSONObject();
        requestObj.put("cAppId", wdId);
        try {
            ApiResModel apiResModel = hanedaApiService.api(new ApiReqModel("secWDOrderBAAPI", requestObj.toString()));
            if (!apiResModel.getSuccess()) {
                log.error("风控端：查询提现订单详情调用失败:" + wdId + "\t" + apiResModel.getErrMessage());
                return null;
            }
            log.info("findWithdrawAppInfoByAppId result" + apiResModel.getResult());
//            wdOrderInfoModel = JSONObject.parseObject(apiResModel.getResult(), WdOrderInfoModel.class);
            Map<String, Object> resultMap = JSON.parseObject(apiResModel.getResult());
            log.info("findWithdrawAppInfoByAppId model" + apiResModel.getResult());
            return resultMap;
        } catch (Exception e) {
            log.error("查询提现订单失败!" + e.getMessage(), e);
            throw new CashChannelException("查询提现订单失败!");
        }
    }

    public Map<String, Object> getWDBYBAAPI(String appId) throws CashChannelException {
        try {
            log.info("风控端：生成提现订单号:" + appId);
            com.alibaba.fastjson.JSONObject requestObj = new com.alibaba.fastjson.JSONObject();
            requestObj.put("cAppId", appId);
            ApiResModel apiResModel = hanedaApiService.api(new ApiReqModel("getWDBYBAAPI", requestObj.toString()));
            if (!apiResModel.getSuccess()) {
                log.error("风控端：生成提现订单号失败:" + appId + "\t" + apiResModel.getErrMessage());
                throw new HanedaException(apiResModel.getErrMessage());
            }
            Map<String, Object> resultMap = Maps.newHashMap();
            resultMap.put("wdId", apiResModel.getResult());
            log.info("风控端：生成提现订单号:" + appId);
            return resultMap;
        } catch (Exception ex) {
            log.error("风控端：生成提现订单号失败:" + appId + "\t" + ex.getMessage(), ex);
            throw new CashChannelException("风控端：生成提现订单号失败:" + appId);
        }
    }

    public String getOrderStatus(WdOrderInfoModel wdOrderInfoModel, Loan loan) {
        String status = null;
        switch (wdOrderInfoModel.getNWdAppStatus()) {
            case 1100:
                status = "4001";
                break;
            case 1200:
                status = "4002";
                break;
            case 1300:
            case 1400:
                status = "4003";
                break;
            case 1500:
                status = "4101";
                break;
            case 1600:
                status = "4102";
                break;
            default:
                break;
        }
        if (loan!=null) {
            if (loan.getIsOverdue().equals(IsOverdueEnum.是)) {
                return "4202";
            }
            if (loan.getLoanStatus().equals(LoanStatusEnum.完成)||loan.getLoanStatus().equals(LoanStatusEnum.提前还款完成)) {
                return "4203";
            }
        }
        return status;
    }
}
